using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace PaulRz.Util
{
	/// <summary>
	/// Special XSLT context which adds support of two function, which are not usually supported in pure XPath 
	/// (but only in cunjunction with XSLT )
	/// </summary>
	public class XSLTDocumentContext: XsltContext
	{
		public XSLTDocumentContext(): base( new NameTable() )
		{
			
		}
		

		public override IXsltContextVariable ResolveVariable(string prefix, string name)
		{
			throw new Exception("Unknown variable");
		}

		
		public override IXsltContextFunction ResolveFunction(string prefix, string name, XPathResultType[] ArgTypes)
		{
			try
			{
				name=name.Substring(0,1).ToUpper()+name.Substring(1);
				Type t=Type.GetType(typeof(XSLTDocumentContext).Namespace+"."+name+"Function",false,true);
				if(t==null)
					throw new Exception("Function hasn't been found");
				IXsltContextFunction function=(IXsltContextFunction) Activator.CreateInstance(t);
				return function;
			}
			catch (Exception e)
			{
				throw new Exception("Error in function invoking: '"+name+"': "+e);				}
		}

		public override bool PreserveWhitespace(XPathNavigator node)
		{
			return true;
		}

		
		private XPathNavigator current;

		/// <summary>
		/// Contains a root node of main document in which was document() function called
		/// </summary>
		public XPathNavigator Current
		{
			get { return current; }
			set { current = value; }
		}

		public override int CompareDocument(string baseUri, string nextbaseUri)
		{
			return 0;
		}

		public override bool Whitespace
		{
			get { return false; }
		}

	}
}